package com.atguigu.controller;


import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.config.AlipayConfig;
import com.atguigu.entity.PaymentInfo;
import com.atguigu.enums.PaymentStatus;
import com.atguigu.result.RetVal;
import com.atguigu.service.PaymentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentInfoService paymentInfoService;

    //1.创建支付二维码  + 支付订单
    @RequestMapping("createQrCode/{orderId}")
    public String createQrCode(@PathVariable Long orderId) throws Exception {
        return paymentInfoService.createQrCode(orderId);
    }

    //2.支付宝异步调用接口
    @PostMapping("/async/notify")
    public String asyncNotify(@RequestParam Map<String,String> aliParamMap) throws Exception {
        //调用SDK验证签名看是支付宝的数据是否正确
        boolean signVerified = AlipaySignature.rsaCheckV1(aliParamMap, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
        //根据商户订单号查询支付订单信息
        String outTradeNo = aliParamMap.get("out_trade_no");
        if(signVerified){
            String tradeStatus = aliParamMap.get("trade_status");
            //交易成功 更改订单信息
            if("TRADE_SUCCESS".equals(tradeStatus)||"TRADE_FINISHED".equals(tradeStatus)){
                //本地支付订单信息
                PaymentInfo paymentInfo = paymentInfoService.getPaymentInfo(outTradeNo);
                //如果我的本地支付订单信息是已经关闭和已经支付 支付宝就不需要再次调用
                if(paymentInfo.getPaymentStatus().equals(PaymentStatus.ClOSED.name())||
                        paymentInfo.getPaymentStatus().equals(PaymentStatus.PAID.name())){
                    return "failure";
                }
                //修改支付订单信息
                paymentInfoService.updatePaymentInfo(aliParamMap);
                return "success";
            }
        }else{
            // TODO 验签失败则记录异常日志，并在response中返回failure.
            log.error("支付失败"+outTradeNo);
            return "failure";
        }
        return "failure";
    }

    //3.发起退款接口
    @RequestMapping("refund/{orderId}")
    public RetVal refund(@PathVariable Long orderId) throws Exception {
        boolean flag=paymentInfoService.refund(orderId);
        return RetVal.ok(flag);
    }
    //4.查询支付宝中是否有交易记录
    @RequestMapping("queryAlipayTrade/{orderId}")
    public boolean queryAlipayTrade(@PathVariable Long orderId) throws Exception {
        return paymentInfoService.queryAlipayTrade(orderId);
    }
    //5.关闭交易
    @RequestMapping("closeAlipayTrade/{orderId}")
    public RetVal closeAlipayTrade(@PathVariable Long orderId) throws Exception {
        boolean flag=paymentInfoService.closeAlipayTrade(orderId);
        return RetVal.ok(flag);
    }
    //6.查询支付订单信息
    @GetMapping("getPaymentInfo/{outTradeNo}")
    public PaymentInfo getPaymentInfo(@PathVariable String outTradeNo) {
        return paymentInfoService.getPaymentInfo(outTradeNo);
    }

}
